<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="login-header">
        <h2>海外数据平台</h2>
        <p>请登录后使用系统功能</p>
      </div>

      <el-form ref="loginFormRef" :model="loginForm" :rules="rules" class="login-form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="User"
            class="no-autofill-bg"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" prefix-icon="Lock"
            class="no-autofill-bg"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login-button" @click="handleLogin" :loading="loading">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { showMessage } from '@/utils/message'

const router = useRouter()
const loading = ref(false)
const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const loginFormRef = ref(null)

const handleLogin = async () => {
  // 表单验证
  if (!loginFormRef.value) return

  try {
    await loginFormRef.value.validate()
    loading.value = true
    // 获取当天日期，格式为YYYYMMDD
    const today = new Date()
    const year = today.getFullYear()
    const month = String(today.getMonth() + 1).padStart(2, '0')
    const day = String(today.getDate()).padStart(2, '0')
    const todayPassword = `${year}${month}${day}...`

    if (loginForm.username === 'admin' && loginForm.password === todayPassword) {
      // 设置登录标记和过期时间（一天后过期）
      const now = new Date()
      const expireTime = new Date(now.getTime() + 24 * 60 * 60 * 1000) // 24小时后过期

      const authData = {
        token: 'mock_token',
        expireTime: expireTime.getTime(),
        loginTime: now.getTime()
      }

      localStorage.setItem('auth_token', JSON.stringify(authData))
      showMessage.success('登录成功')
      router.push('/')
    } else {
      showMessage.error('登录失败，请检查用户名和密码')
    }
  } catch (error) {
    showMessage.error('登录失败，请检查用户名和密码')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa;
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 400px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
  overflow: hidden;
}

.login-header {
  text-align: center;
  padding: 24px 0;
  border-bottom: 1px solid #eee;
}

.login-header h2 {
  margin: 0 0 8px 0;
  color: #1f2329;
  font-size: 20px;
}

.login-header p {
  margin: 0;
  color: #86909c;
  font-size: 14px;
}

.login-form {
  padding: 24px;
}

.login-button {
  width: 100%;
}

/* 针对原生input和el-input内部的input，清除自动填充背景 */
.no-autofill-bg :deep(input) {
  /* 基础样式重置 */
  background-color: #fff !important;
}

/* 关键：覆盖浏览器自动填充的伪类样式 */
.no-autofill-bg :deep(input:-webkit-autofill) {
  /* 清除背景色和文字颜色 */
  -webkit-background-clip: text;
  -webkit-text-fill-color: #333 !important;
  background-color: transparent !important;

  /* 禁用自动填充的动画效果 */
  transition: background-color 5000s ease-in-out 0s !important;
}

/* 处理聚焦时的自动填充状态 */
.no-autofill-bg :deep(input:-webkit-autofill:focus) {
  background-color: transparent !important;
  -webkit-text-fill-color: #333 !important;
}
</style>